package com.neoyr.meeting.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neoyr.meeting.common.Result;
import com.neoyr.meeting.entity.User;
import com.neoyr.meeting.entity.UserDetail;
import com.neoyr.meeting.entity.vo.UserVo;
import com.neoyr.meeting.service.Imp.UserDetailImp;
import com.neoyr.meeting.service.Imp.UserImp;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserImp userImp;
    @Autowired
    public UserDetailImp userDetailImp;

    @GetMapping
    public Result<List> getList() {
        Page<User> page = new Page<>();
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        List<User> list = userImp.list(query);
        return Result.success(list);
    }



    @PostMapping
    public Result<String> add(@RequestBody User user) {
        userImp.saveUser(user);
        return Result.success("添加成功");
    }

    @GetMapping("/{id}")
    public Result<UserVo> info(@PathVariable Long id) {
        System.out.println("::::::::"+id);
        User user = userImp.getById(id);
        System.out.println(user);
        LambdaQueryWrapper<UserDetail> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();

      objectLambdaQueryWrapper.eq(UserDetail::getUserId, id);
     UserDetail userDetail = userDetailImp.getOne(objectLambdaQueryWrapper);
        Map<String, Object> mergedMap = new HashMap<>();
//        Field[] filelds=user.getClass().getDeclaredFields();

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        userVo.setUserDetail(userDetail);
//        mergedMap.put("user", user);
//        mergedMap.put("userDetail", userDetail);

    return  Result.success(userVo);
    }

    @PutMapping
    public Result<String> update(@RequestBody User user) {
        UpdateWrapper<User> query = new UpdateWrapper<>();

        userImp.update(query);
        return Result.success("修改成功");
    }

}


